package com.module.admin.controller;

import com.module.admin.Warpper.UserWarpper;
import com.module.common.factory.ConstantFactory;
import com.module.core.shiro.ShiroUser;
import com.module.model.UserInfo;
import com.module.service.UserService;
import com.module.user.domain.BaseResp;
import com.module.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/mgr")
public class AdminController {

    @Autowired
    private UserService userService;

    private static String PREFIX = "/back/system/user/";

    @RequestMapping("")
    public String index() {
        return PREFIX + "user.btl";
    }


    @RequestMapping("/user_add")
    public String addView() {
        return PREFIX + "user_add.btl";
    }


    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, @RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime) {
        return new UserWarpper(userService.list(name,beginTime,endTime)).warp();
    }

    @RequestMapping("/getInfo")
    @ResponseBody
    public Object getInfo() {
        Map<String,Object> data=new HashMap<>();
        ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        String username = user.account;
        data.put("name", username);
        return data;
    }
    /**
     * 跳转到查看用户详情页面
     */
    @RequestMapping("/user_info/{userId}")
    public String userInfo(Model model,@PathVariable Integer userId) {
        UserInfo userInfo= this.userService.selectById(userId);
        model.addAttribute("userInfo",userInfo);
        return PREFIX + "user_view.btl";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public BaseResp<String> edit( UserInfo userInfo){
        userService.modifyUser(userInfo);
        BaseResp resp = new BaseResp();
        resp.setStatus(0);
        resp.setMessage("修改成功");
        return resp;
    }

    @RequestMapping("/add")
    @ResponseBody
    public BaseResp<String> add( UserInfo userInfo){
        userInfo.setRoleid(2);
        userInfo.setStatus(2);
        userInfo.setCreatetime(DateUtil.parse(DateUtil.getNow()));
        userService.insert(userInfo);
        BaseResp resp = new BaseResp();
        resp.setStatus(0);
        resp.setMessage("增加成功");
        return resp;
    }


    @RequestMapping("/delete/{userId}")
    @ResponseBody
    public BaseResp<String> delete(@PathVariable Integer userId ) {
        UserInfo info=new UserInfo();
        info.setId(userId);
        info.setStatus(1);
        info.setUpdatetime(DateUtil.parse(DateUtil.getNow()));
        userService.modifyUser(info);
        BaseResp resp = new BaseResp();
        resp.setStatus(0);
        resp.setMessage("删除成功");
        return resp;
    }


}
